package io.sage.modules.sensor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.sage.common.utils.R;
import io.sage.modules.sensor.entity.SysSensorConfiguration;
import io.sage.modules.sensor.service.SensorConfigurationService;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.shiro.ShiroUtils;

/**
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2019-06-20 13:51:50
 */
@RestController
@RequestMapping("sensor/sensorconfiguration")
public class SensorConfigurationController {
	
    @Autowired
    private SensorConfigurationService sensorConfigurationService;

    /**
     * 列表
     */
    @ResponseBody
	@GetMapping("/list")
    @RequiresPermissions("sensor:sensorconfiguration:list")
	public R list(@RequestParam Map<String, Object> params) {
		Integer pageSize = 1;
		if (params != null && params.get("limit") != null) {
			pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
		}
		Integer pageNum = 10;
		if (params != null && params.get("page") != null) {
			pageNum = java.lang.Integer.parseInt(params.get("page").toString());
		}

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		System.out.println(params.get("examinationName"));
		if(params.get("examinationName")!=null){

			if(!"".equals(params.get("examinationName").toString())){
				paramMap.put("examinationName", params.get("examinationName").toString());
			}
		}

		if(params.get("studentName")!=null){
			if(!"".equals(params.get("studentName").toString())){
				paramMap.put("studentName", params.get("studentName").toString());
			}
		}

		if(params.get("deptNo")!=null){
			if(!"".equals(params.get("deptNo").toString())){
				paramMap.put("deptNumber", params.get("deptNo").toString());
			}
		}

		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小

		List<SysSensorConfiguration> selectStudentLists = sensorConfigurationService.selectStudentLists(paramMap);

		PageInfo<SysSensorConfiguration> pageInfo = new PageInfo<SysSensorConfiguration>(selectStudentLists);// 传入list就可以了

		return R.ok().put("sensorConfiguration", pageInfo);
	}


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sensor:sensorconfiguration:info")
    public R info(@PathVariable("id") Integer id){
    	
    	SysSensorConfiguration sensorConfiguration = sensorConfigurationService.selectById(id);

        return R.ok().put("sensorConfiguration", sensorConfiguration);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sensor:sensorconfiguration:save")
    public R save(@RequestBody JSONObject jsonObject){
    	
    	SysSensorConfiguration sensorConfiguration = jsonObject.getObject("sensorConfiguration", SysSensorConfiguration.class);
		
    	SysUserEntity user = ShiroUtils.getUserEntity();
    	sensorConfiguration.setUserId(user.getUserId().intValue());
    	
        int insert = sensorConfigurationService.insert(sensorConfiguration);
        
        if(insert>0){
			return R.ok();
		}

		return R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sensor:sensorconfiguration:update")
    public R update(@RequestBody JSONObject jsonObject){
    	
    	SysSensorConfiguration sensorConfiguration = jsonObject.getObject("sensorConfiguration", SysSensorConfiguration.class);
		
    	SysUserEntity user = ShiroUtils.getUserEntity();
    	sensorConfiguration.setUserId(user.getUserId().intValue());
    	
    	int update = sensorConfigurationService.updateAllColumnById(sensorConfiguration);//全部更新
        
    	if(update>0){
			return R.ok();
		}
		return R.error();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sensor:sensorconfiguration:delete")
    public R delete(@RequestBody Integer[] ids){
    	
    	int delete = sensorConfigurationService.deleteBatchIds(ids);

		if(delete>0){
			return R.ok();
		}

		return R.error();
    }
    /**
     * 停用/启用
     * @param jsonObject
     * @return
     */
    @RequestMapping("/stopOrstart")
	public R stopOrstart(@RequestBody JSONObject jsonObject){
		
		Integer id = jsonObject.getInteger("id");
		Integer disFlag = jsonObject.getInteger("disFlag");
		
		if(id==null || disFlag == null){
			return R.error("参数不正确");
		}
		int update = sensorConfigurationService.stopOrstart(id,disFlag);
		
		if(update>0){
			return R.ok();
		}
		return R.error();
	}

}
